def jwt_response_payload_handler(token,user=None,request=None):
    '''
    拓展jwt返回数据的功能
    :param token:  jwt token字符串
    :param user:  当前的登录对象。model类型的对象
    :param request: 当前的请求对象
    :return:
    '''

    return {
        'token':token,
        'username':user.username,
        'id':user.id,
    }

# def get_user_by_account(account):

from django.contrib.auth.backends import ModelBackend
from . import models
from rest_framework.response import Response
import re


# 判断手机或者用户名登录方法
def get_user_by_account(account):

    try:
        user = models.User.objects.get(mobile=account) if re.match(r'^1[3-9]\d{9}$', account) \
            else models.User.objects.get(username=account)
    except models.User.DoesNotExist:
        return

    return user


class UsernameMobileAuthBackend(ModelBackend):
    '''
    重构jwt的登录功能，可以允许多条件登录。
    '''
    def authenticate(self, request, username=None, password=None, **kwargs):
        user = get_user_by_account(username)
        print(user.username)
        if user is not None and user.check_password(password):

            return user